<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WEB SOCKET 客户端</title>
</head>
<body>

<script type="text/javascript">
    var socket;

    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:18899/ws");

        socket.onmessage = function (event) {
            var ta = document.getElementById('responseText');
            ta.value += "\n" + event.data;
        }

        socket.onopen = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = "Connection established."
        }

        socket.onclose = function (event) {
            var ta = document.getElementById('responseText');
            ta.value += "\n Connection closed."
        }
    } else {
        alert('Unsupported')
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState === WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert('连接尚未开启!');
        }
    }
</script>

<form onsubmit="return false;">
    <label>
        <textarea name="message" style="width: 400px; height: 200px;"></textarea>
    </label>
    <input type="button" value="send" onclick="send(this.form.message.value)">

    <h3>服务端:</h3>
    <label for="responseText"></label><textarea id="responseText" style="width: 400px; height: 300px;"></textarea>
    <input type="button" value="clean" onclick="document.getElementById('responseText').value = ''">
</form>

</body>
</html>